//////////////////////////////////////////////////////////////////////////

#ifndef _EDIT_H_
#include "edit.h"
#endif

//////////////////////////////////////////////////////////////////////////

#ifndef _THEME_H_
#include "theme.h"
#endif

#ifndef _IMAGES_H_
#include "images.h"
#endif

#ifndef _SURFACE_H_
#include "surface.h"
#endif

#ifndef _COLORS_H_
#include "colors.h"
#endif

#ifndef _FONTS_H_
#include "fonts.h"
#endif

#ifndef _SOUNDS_H_
#include "sounds.h"
#endif

#ifndef _IMAGESURFACE_H_
#include "imagesurface.h"
#endif

#ifndef _TEXTSURFACE_H_
#include "textsurface.h"
#endif

#ifndef _INPUT_H_
#include "input.h"
#endif

#ifndef _SETTINGS_H_
#include "settings.h"
#endif

#include <SDL\SDL.h>
#include <SDL_mixer.h>

////////////////////////////////////////////////////////////////////////////////


GUI_Edit::GUI_Edit(GUI_Images::Image _backgrount, GUI_Fonts::Font _font, GUI_Colors::Color _color, const std::string& _text, GUI_Images::Image _hover, float _left, float _top):
    m_ShiftPressed(false)
{
    m_Background = new GUI_ImageSurface(_backgrount);
    m_Text = new GUI_TextSurface(_font, _color, _text);
    m_Hover = new GUI_ImageSurface(_hover);

    m_HasHover = false;
    m_HasFocus = false;

    m_XPosition = _left;
    m_YPosition = _top;
    

    m_TextAllign = GUI_Surface::AllignLeft;
    m_HorisontalMargin = - m_Background->Width() / 2 + 0.01;
    m_VerticalMargin = 0;
}


////////////////////////////////////////////////////////////////////////////////


GUI_Edit::~GUI_Edit()
{
    delete m_Background;
    delete m_Text;
    delete m_Hover;
}


////////////////////////////////////////////////////////////////////////////////


GUI_Edit::InputResult GUI_Edit::OnKeyChanged(Input* _input)
{
    Input::Key key = _input->GetKey(); 
    if (key.m_State == Input::MOUSE_RELEASED)
    {
        switch (key.m_Code)
        {
        case Input::MOUSE1:
            if (m_HasHover)
            {
                m_HasFocus = true;
                Mix_PlayChannel(-1, GUI_Theme::GetSound(GUI_Sounds::Hover), 0);
                return IR_GetFocus;
            }
            break;
        default:
            break;
        }
    }
    if (key.m_State == Input::PRESSED)
    {
        if (m_HasFocus)
        {
            switch (key.m_Code)
            {
            case Input::LSHIFT:
                m_ShiftPressed = true;
                break;
            default:
                break;
            }
        }
    }
    if (key.m_State == Input::RELEASED)
    {
        if (m_HasFocus)
        {
            Mix_PlayChannel(-1, GUI_Theme::GetSound(GUI_Sounds::Hover), 0);
            switch (key.m_Code)
            {
            case Input::LSHIFT:
                m_ShiftPressed = false;
                break;
            case Input::BACKSPACE:
                {
                    std::string Value = m_Text->GetText();
                    if (Value.size() > 0)
                    {
                        Value.erase(Value.size()-1);
                        m_Text->SetText(Value);
                        return IR_NeedRepaint;
                    }
                }
                break;
            default:
                {
                    std::string Value = m_Text->GetText();
                    if (Value.size() < MAX_LENGTH)
                    {
                        Value.push_back((m_ShiftPressed && (key.m_Code > 0x60) && (key.m_Code < 0x7B)) ? (key.m_Code - 0x20) : (key.m_Code));
                        m_Text->SetText(Value);
                        return IR_NeedRepaint;
                    }
                }
                break;
            }
        }
    }
    return IR_NoChanges;
}


////////////////////////////////////////////////////////////////////////////////
